//
//  LLPopView.m
//  Weibo
//
//  Created by 李灵斌 on 15-1-30.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "LLPopView.h"

@implementation LLPopView

// 全局变量在程序结束后才会销毁
// 局部变量方法执行完，就会销毁
static UIWindow *_window;
static dismissBlock _dismiss;
static UIViewController *_Vc;
//static UIButton *_cover;
+ (void)popViewForView:(UIView *)view content:(UIView *)content dismiss:(dismissBlock)dismiss{
    
    _dismiss = [dismiss copy];
    // 1,创建window
    _window = [[UIWindow alloc] init];
    // 默认创建的window是隐藏的
    _window.hidden = NO;
    // 设置window的优先级
    _window.windowLevel = UIWindowLevelAlert;
    _window.frame = [UIScreen mainScreen].bounds;
    
    // 2,创建蒙版添加到window上
    UIButton *cover = [[UIButton alloc] init];
    [_window addSubview:cover];
    cover.backgroundColor = [UIColor clearColor];
    cover.frame = _window.frame;
    [cover addTarget:self action:@selector(coverClick) forControlEvents:UIControlEventTouchUpInside];
    
    // 3,创建图片
    UIImageView *imageView = [[UIImageView alloc] init];
    [cover addSubview:imageView];
    imageView.image = [UIImage stretchableImageWithImage:@"popover_background"];
    imageView.userInteractionEnabled = YES;
    
    // 添加动画：
    [UIView animateWithDuration:1.0 animations:^{
        
        imageView.transform = CGAffineTransformMakeScale(0.99, 0.99);
    } completion:^(BOOL finished) {
        
            imageView.transform = CGAffineTransformIdentity;

    }];
    
    // 根据content设置imageView内容
    content.x = 10;
    content.y = 20;
    [imageView addSubview:content];
    
    imageView.frame = CGRectMake(0, 0, content.size.width + 2 * content.x, content.size.height + content.y * 1.6);
//    CGPoint center = [_window convertPoint:view.center toView:view.superview];
    CGRect resultFrame = [_window convertRect:view.frame fromView:view.superview];
    
    imageView.centerX = resultFrame.origin.x + resultFrame.size.width * 0.5;
    imageView.y = CGRectGetMaxY(resultFrame);
}

+ (void)popViewForView:(UIView *)view contentVc:(UIViewController *)contentVc dismiss:(dismissBlock)dismiss type:(LLPopViewType)type{
    UIView *content = contentVc.view;
    _dismiss = [dismiss copy];
    // 1,创建window
    _window = [[UIWindow alloc] init];
    // 默认创建的window是隐藏的
    _window.hidden = NO;
    // 设置window的优先级
    _window.windowLevel = UIWindowLevelAlert;
    _window.frame = [UIScreen mainScreen].bounds;

    // 2,创建蒙版添加到window上
    UIButton *cover = [[UIButton alloc] init];
    [_window addSubview:cover];
    cover.backgroundColor = [UIColor clearColor];
    cover.frame = _window.frame;
    [cover addTarget:self action:@selector(coverClick) forControlEvents:UIControlEventTouchUpInside];
    
    // 3,创建图片
    UIImageView *imageView = [[UIImageView alloc] init];
    [cover addSubview:imageView];
    imageView.userInteractionEnabled = YES;

    NSString *imageStr = nil;
    
    content.x = 10;
    content.y = 20;
    [imageView addSubview:content];
    
    imageView.frame = CGRectMake(0, 0, content.size.width + 2 * content.x, content.size.height + content.y * 2);
    //    CGPoint center = [_window convertPoint:view.center toView:view.superview];
    CGRect resultFrame = [_window convertRect:view.frame fromView:view.superview];
    
//    imageView.centerX = resultFrame.origin.x + resultFrame.size.width * 0.5 - 35;
    imageView.y = CGRectGetMaxY(resultFrame);
    switch (type) {
        case LLPopViewTypeMiddle:
            imageStr = @"popover_background";
            imageView.centerX = resultFrame.origin.x + resultFrame.size.width * 0.5;            break;
        case LLPopViewTypeLeft:
            imageStr = @"popover_background_left";
            imageView.x = resultFrame.origin.x;
            break;
        case LLPopViewTypeRight:
            imageStr = @"popover_background_right";
            imageView.centerX = CGRectGetMaxX(resultFrame) - imageView.frame.size.width * 0.5;
            break;
        default:
            break;
    }
    imageView.image = [UIImage stretchableImageWithImage:imageStr];
    
//    // 添加动画：
//    [UIView animateWithDuration:1.0 animations:^{
//        
//        imageView.transform = CGAffineTransformMakeScale(0.99, 0.99);
//    } completion:^(BOOL finished) {
//        
//        imageView.transform = CGAffineTransformIdentity;
//        
//    }];
    
    // 根据content设置imageView内容
    
}

+ (void)popViewForView:(UIView *)view contentVc:(UIViewController *)contentVc dismiss:(dismissBlock)dismiss{
    
    _Vc = contentVc;
    [self popViewForView:view content:_Vc.view dismiss:dismiss];
}
+ (void)coverClick{
    
    _window = nil;
    _Vc = nil;
    if (_dismiss == nil)return;
    _dismiss();
}



@end
